package com.angel.activity;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.angel.entity.Task;
import com.angel.port.IActivity;
import com.angel.util.BaseUtil;
import com.angel.util.UserInfoUtil;
import com.umeng.analytics.MobclickAgent;
import com.weibo.net.AccessToken;
import com.weibo.net.DialogError;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboDialogListener;
import com.weibo.net.WeiboException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class SetUpActivity extends Activity implements IActivity{

	/** 设置按钮 */
	private Button return_btn;
	private Button outsign_btn;
	private RelativeLayout  feekbackBnt, set_weibo_bindingBtn,
			set_qq_binding, set_phone_binding, set_email_binding,set_username_binding, contract,
			set_about_us,activity_tips;
	private MyListener listener;
	private TextView nameTxt,notifyTime, isWeiboBind, isQQBind,isPhoneBind,isEmailBind;
	private Context context;
	private Intent tent;
	private HashMap<String,String> map=new HashMap<String,String>();
	private ProgressDialog progressDialog;
	private String userName;//用户名
	private int remindTime;//提醒时间天数
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings_activity);
		 MobclickAgent.onError(this);//自动捕获异常退出（FC ）
		
		context = SetUpActivity.this;
		progressDialog=new ProgressDialog(context);
		progressDialog.setMessage("请稍后");
		
		return_btn = (Button) findViewById(R.id.return_btn);
		outsign_btn = (Button) findViewById(R.id.outsign_btn);
//		reLayout = (RelativeLayout) findViewById(R.id.feekback_layoutid);
		feekbackBnt = (RelativeLayout) findViewById(R.id.feekback_layoutid);
		set_weibo_bindingBtn = (RelativeLayout) findViewById(R.id.set_weibo_binding);
		set_qq_binding = (RelativeLayout) findViewById(R.id.set_qq_binding);
		set_phone_binding = (RelativeLayout) findViewById(R.id.set_phone_binding);
		set_email_binding = (RelativeLayout) findViewById(R.id.set_email_binding);
		set_username_binding=(RelativeLayout)findViewById(R.id.set_username_binding);
		
		set_about_us = (RelativeLayout) findViewById(R.id.set_about_us);
		activity_tips = (RelativeLayout) findViewById(R.id.activity_tips);

		nameTxt = (TextView) findViewById(R.id.set_username_id);
		notifyTime=(TextView)findViewById(R.id.notify_time_txt);
		isWeiboBind = (TextView) findViewById(R.id.weibo_no_binding);
		isQQBind = (TextView) findViewById(R.id.qq_no_binding);
		isPhoneBind=(TextView)findViewById(R.id.phone_no_binding);
		isEmailBind=(TextView)findViewById(R.id.email_no_binding);
		
		contract = (RelativeLayout) findViewById(R.id.contract);
		
		listener = new MyListener();

		set_weibo_bindingBtn.setOnClickListener(listener);
		return_btn.setOnClickListener(listener);
		outsign_btn.setOnClickListener(listener);
//		reLayout.setOnClickListener(listener);
		feekbackBnt.setOnClickListener(listener);
		set_qq_binding.setOnClickListener(listener);
		set_phone_binding.setOnClickListener(listener);
		set_email_binding.setOnClickListener(listener);
		set_username_binding.setOnClickListener(listener);
		set_username_binding.setLongClickable(true);
		
		contract.setOnClickListener(listener);
		set_about_us.setOnClickListener(listener);
		activity_tips.setOnClickListener(listener);
		
		initRemindTime();
	}
	
	/**
	 * 初始化提醒时间 
	 */
	public void initRemindTime(){
		remindTime=Integer.parseInt(UserInfoUtil.getValue(context, "remindTime", "0"));
		switch(remindTime){
		case 1:
			notifyTime.setText("提前一天");
			break;
		case 2:
			notifyTime.setText("提前二天");
			break;
		case 3:
			notifyTime.setText("提前三天");
			break;
		default:
			notifyTime.setText("提前一天");
			break;
		}
			
	}

	class MyListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			int id = v.getId();
			AlertDialog.Builder builder = new AlertDialog.Builder(
					SetUpActivity.this);
			if (id == R.id.return_btn) {
				finish();
            }else if (id == R.id.feekback_layoutid) {
				startActivity(new Intent(SetUpActivity.this, FeekBack.class));
			} else if (id == R.id.outsign_btn) {// 退出当前账号
				BaseUtil.showChangeDialog(SetUpActivity.this, context);
			}else if (id == R.id.set_weibo_binding) {//设置绑定微博
				tent = new Intent(SetUpActivity.this, SinaWeibo.class);
				tent.putExtra("isBind", isWeiboBind.getText().toString());
				startActivity(tent);
			} else if (id == R.id.set_qq_binding) {
				tent = new Intent(SetUpActivity.this, QQBinding.class);
				tent.putExtra("isBind", isQQBind.getText().toString());
				startActivity(tent);
			} else if (id == R.id.set_phone_binding) {
				tent =new Intent(SetUpActivity.this, Register1Activity.class);
				tent.putExtra("isBind", "1");
				startActivity(tent);
			} else if (id == R.id.set_email_binding) {
				startActivity(new Intent(SetUpActivity.this, EmailBinding.class));
			} else if (id == R.id.contract) {
				
				builder.setTitle("用户协议");
				builder.setMessage(R.string.contract);

				builder.setPositiveButton("返回",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();
							}
						});
				builder.show();
			} else if(id == R.id.set_about_us){
				startActivity(new Intent(SetUpActivity.this, SettingAboutUs.class));
				MobclickAgent.onEvent(SetUpActivity.this, "aboutUs");//发送点击关于我们统计到友盟
			}else if(id==R.id.set_username_binding){//更改用户名
				MobclickAgent.onEvent(SetUpActivity.this, "modifyName");//发送修改用户名统计到友盟
				final EditText editname=new EditText(SetUpActivity.this);
				
				editname.setSingleLine(true);
				builder.setTitle("请输入新用户名").
				setView(editname).
				setPositiveButton("确定",new  DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						userName=editname.getText().toString();
						map.put("userName", userName);
						map.put("userId", UserInfoUtil.getValue(context, "userId", "0"));
						BaseUtil.startService(Task.CHANGESETTING, SetUpActivity.this, map);
						progressDialog.show();
					}}
				).setNegativeButton("取消", null).
				show();
			}
			else if(id == R.id.activity_tips){//活动提醒
				builder.setTitle("请选择")
				.setIcon(R.drawable.green_light)
				.setItems(new String[] {"提前一天","提前两天","提前三天"}, new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						switch(which){
						case 0:
							notifyTime.setText("提前一天");
							map.put("remindTime", "1");
						break;
						case 1:
							notifyTime.setText("提前两天");
							map.put("remindTime", "2");
							break;
						case 2:
							notifyTime.setText("提前三天");
							map.put("remindTime", "3");
							break;
						}
						map.put("userId", UserInfoUtil.getValue(context, "userId", "0"));
						BaseUtil.startService(Task.UPDATEREMINDTIME, SetUpActivity.this, map);
						progressDialog.show();
						dialog.dismiss();
					}
				})
				.setNegativeButton("取消", null)
				.show();
			}
		}
	}

	@Override
	protected void onStart() {
		//获取判断是否为手机号登陆
		String isAccountLogin=UserInfoUtil.getValue(context, "isAccountLogin", "0");
		// 设置用户名
		nameTxt.setText("用户名: "
				+ UserInfoUtil.getValue(context, "userName", "匿名"));
		String weiboAccessToken = UserInfoUtil.getValue(context,
				"uid", "");
		String qqAccessToken = UserInfoUtil.getValue(context,
				"openid", "");
		String phoneNum=UserInfoUtil.getValue(context, "phoneAccount", "");
		String email=UserInfoUtil.getValue(context, "email", "");
		/* 微博绑定状态 */
		if (!weiboAccessToken.equals("")) {
			isWeiboBind.setText("已绑定");
			isWeiboBind.setTextColor(getResources().getColor(R.color.red));
		} else {
			isWeiboBind.setText("未绑定");
			isWeiboBind.setTextColor(getResources().getColor(R.color.black));
		}
		/*qq绑定状态 */
		if (!qqAccessToken.equals("")) {
			isQQBind.setText("已绑定");
			isQQBind.setTextColor(getResources().getColor(R.color.red));
		} else {
			isQQBind.setText("未绑定");
			isQQBind.setTextColor(getResources().getColor(R.color.black));
		}
		/*手机号绑定状态，若绑定,则不能再点击*/
		if(!phoneNum.equals("")){
			isPhoneBind.setText("已绑定");
			isPhoneBind.setTextColor(getResources().getColor(R.color.red));
			set_phone_binding.setEnabled(false);
		}else{
			isPhoneBind.setText("未绑定");
			isPhoneBind.setTextColor(getResources().getColor(R.color.black));
		}
		/*邮箱绑定*/
		if(!email.equals("")){
			isEmailBind.setText("已绑定");
			isEmailBind.setTextColor(getResources().getColor(R.color.red));
			set_email_binding.setEnabled(false);
		}else{
			isEmailBind.setText("未绑定");
			isEmailBind.setTextColor(getResources().getColor(R.color.black));
		}
		//若为手机号登陆，则不能再进行微博或者qq绑定
		if(isAccountLogin.equals("1")){
			set_qq_binding.setEnabled(false);
			set_weibo_bindingBtn.setEnabled(false);
		}
		//若为微博登陆，则不能再进行qq绑定
		if(isAccountLogin.equals("2")){
			set_qq_binding.setEnabled(false);
		}
		//若为qq登陆，则不能再进行微博绑定
		if(isAccountLogin.equals("3")){
			set_weibo_bindingBtn.setEnabled(false);
		}
		super.onStart();
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void refresh(Object... params) {
		
			String isSucess=BaseUtil.getValueFromJSON((String)params[0]);
			
			 if(isSucess.equals("2")){
				 BaseUtil.makeToast(SetUpActivity.this, "对不起,网络连接超时,请稍后重试...");
				 progressDialog.dismiss();
				 return ;
			}
			if(isSucess.equals("3")){
				map=new HashMap<String,String>(1);
				map.put("remindTime", String.valueOf(remindTime));
				UserInfoUtil.setValue(context, map);
				BaseUtil.makeToast(this, "成功修改活动提醒时间");
			}
			else if(isSucess.equals("1")){//若绑定成功，则保存用户名
				map=new HashMap<String,String>();
				map.put("userName", userName);
				UserInfoUtil.setValue(context, map);
				nameTxt.setText("用户名: "+userName);
				BaseUtil.makeToast(this, "成功修改用户名");
			}else{
				BaseUtil.makeToast(this, "对不起,网络出错,请稍后重试...");
			}
		    progressDialog.dismiss();
		} 
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		 MobclickAgent.onResume(this);
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		 MobclickAgent.onPause(this);
	}
}
